﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using LotteryAdmin.AdminService;
namespace LotteryAdmin
{
    public partial class ChangeOdds : ChildWindow
    {
        LotteryAdminClient srv;
        public ChangeOdds()
        {
            InitializeComponent();
            srv = WcfProxy.GetProxy;
            srv.UpdateDefaultOddsCompleted += new EventHandler<UpdateDefaultOddsCompletedEventArgs>(srv_UpdateDefaultOddsCompleted);
            srv.UpdateCurrentOddsCompleted += new EventHandler<UpdateCurrentOddsCompletedEventArgs>(srv_UpdateCurrentOddsCompleted);
        }

        void srv_UpdateCurrentOddsCompleted(object sender, UpdateCurrentOddsCompletedEventArgs e)
        {
            if (e.Result)
            {
                org_odds.NumOdds = _odds.NumOdds;
                org_odds.AllCount = _odds.AllCount;
                this.DialogResult = true;
            }
            else
                MessageBox.Show("操作失败，网络异常或者服务器不可连接");
        }

        void srv_UpdateDefaultOddsCompleted(object sender, UpdateDefaultOddsCompletedEventArgs e)
        {
            if (e.Result)
            {
                org_odds.NumOdds = _odds.NumOdds;
                org_odds.AllCount = _odds.AllCount;
                this.DialogResult = true;
            }
            else
                MessageBox.Show("操作失败，网络异常或者服务器不可连接");
        }
        Odds _odds;
        Odds org_odds;
        bool isDefault = true;
        public void ShowDefaultOdds(Odds odds)
        {
            org_odds = odds;
            _odds = ModelCopy.CopyValueForModel<Odds>(odds);
            this.Title = odds.LotteryName + "默认赔率修改";
            this.Title += ("->" + odds.PlayTypeName + "->" + odds.PlayTypeRadioName);
            this.dataForm1.CurrentItem = _odds;
            this.dataForm1.Loaded += (s, e) =>
            {
                (this.dataForm1.FindNameInContent("df_RemainCount") as DataField).Visibility = Visibility.Collapsed;
                (this.dataForm1.FindNameInContent("df_Odds") as DataField).Label = "默认赔率";
            };
            base.Show();

        }
        public void ShowCurrentOdds(Odds odds)
        {
            isDefault = false;
            org_odds = odds;
            _odds = ModelCopy.CopyValueForModel<Odds>(odds);
            this.Title = odds.LotteryName + "[" + odds.IssueNo + "]期赔率修改";
            this.Title += ("->" + odds.PlayTypeName + "->" + odds.PlayTypeRadioName);
            this.dataForm1.CurrentItem = _odds;
            this.dataForm1.Loaded += (s, e) =>
            {
                (this.dataForm1.FindNameInContent("df_AllCount") as DataField).Visibility = Visibility.Collapsed;
                (this.dataForm1.FindNameInContent("df_Odds") as DataField).Label = "当前期赔率";
            };
            base.Show();
        }
        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (this.dataForm1.ValidateItem())
            {
                if (isDefault)
                    srv.UpdateDefaultOddsAsync(_odds);
                else
                    srv.UpdateCurrentOddsAsync(_odds);
            }
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
    }
}

